#include "ProcessList.h"

void createProcess(ProcessList **processList, int PID, int baseRegister, int timeSlice)
{
	if(*processList == NULL)
	{
		*processList = (ProcessList *) malloc(sizeof(ProcessList));
		(*processList)->next = *processList;
		(*processList)->prev = *processList;
		
		(*processList)->process.PID = PID;
		(*processList)->process.PC = 0;
		(*processList)->process.baseRegister = baseRegister;
		(*processList)->process.timeSlice = timeSlice;
	}
	else
	{
		ProcessList *newProcess = (ProcessList *) malloc(sizeof(ProcessList));
		newProcess->next = *processList;
		newProcess->prev = (*processList)->prev;
		(*processList)->prev->next = newProcess;
		(*processList)->prev = newProcess;
		
		newProcess->process.PID = PID;
		newProcess->process.PC = 0;
		newProcess->process.baseRegister = baseRegister;
		newProcess->process.timeSlice = timeSlice;		
	}
}

void removeProcess(ProcessList **processList)
{
	if(*processList == NULL)
	{
		printf("Error on removeProcess(): The processList is empty!\n");
		exit(-1);
	}
	else
	{
		ProcessList *toBeRemovedProcess = *processList;
		*processList = (*processList)->next;
		(*processList)->prev->prev->next = *processList;
		(*processList)->prev = (*processList)->prev->prev;
		
		free(toBeRemovedProcess);
	}
}


void moveProcess(ProcessList **moveFrom, ProcessList **moveTo)
{/*
	if(moveTo == NULL)
	{
		moveFrom->prev = moveFrom->next;
		moveFrom->next = moveFrom->prev;
		
		moveTo = moveFrom;		
		moveTo->next = moveTo;
		moveTo->prev = moveTo;
	}
	else
	{
		moveFrom->prev = moveFrom->next;
		moveFrom->next = moveFrom->prev;
		
		moveFrom->next = moveTo;
		moveFrom->prev = moveTo->prev;
		
		moveTo->prev->next = moveFrom;
		moveTo->prev = moveFrom;
	}*/
}
